<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2018/12/4
 * Time: 16:33
 * 电台管理模块
 */

namespace app\api\controller;


use app\common\logic\Logic;
use app\common\model\RoomBackground;
use app\common\model\RoomFollow;
use app\common\service\AppService;
use think\Db;
use wheat\Wheat;

class Radio extends User
{

    //是否自由上麦
    protected static $is_free = 1;

    //房间信息
    protected static $roomInfo;

    //房间id
    protected static $room_id;

    //房间类型
    protected static $roomType = [1];

    //普通成员不能上的麦位
    protected static $generalNotUp = [1];

    //是否返回麦位信息
    protected static $isReturn = false;


    public function _initialize()
    {
        parent::_initialize();
        $room_id = input('post.room_id');
        if (!isInt($room_id)) api_return(0,'房间id错误');
        self::$room_id = $room_id;
        self::$roomInfo = $this->roomInfo(self::$room_id);
        $this->roomType();
        if (self::$roomInfo['is_free'] == 0){
            self::$is_free = 0;
        }

        // 获取数据库中用户当前房间id  如果和当前请求房间id不一致修改为当前房间id
        // 为解决融云离线后退出房间用户再次打开app不能在房间在线用户列表出现
        $userInfo = $this->userExtra('room_id,online_status');

        $update = [];

        //调用接口时如果用户不在线  修改为在线状态
        if ($userInfo['online_status'] == 0){
            $update['online_status'] = 1;
        }

        //调用接口时传人了房间id   说明用户在房间内   判断当前数据库数据和传入数据是否一致   不一致退出之前房间
        if ($userInfo['room_id'] != $room_id){

            self::outRoomFunc($this->user_id,$userInfo['room_id']);
            $update['room_id'] = $room_id;
        }

        if (!empty($update)){
            $update['user_id'] = $this->user_id;
            $GLOBALS['EXTEND'] = AppService::pushGlobal('EXTEND',$update);
//            self::updateUserExtend($this->user_id,$update);
        }

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间类型拦截
     */
    protected function roomType(){

        if (!in_array(self::$roomInfo['type'],self::$roomType)){
            api_return(0,'房间类型访问错误');
        }
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 封麦|开麦
     */
    public function wheatChange()
    {
//        $this->checkOwner();
        $this->checkPower();

        $data = request()->only(['wheat_id','type'],'post');

        $wheat = new Wheat();

        if ($data['type'] == 1){ //封麦
            $ret = $wheat->lock(self::$room_id,$data['wheat_id']);
        }else{ //开麦
            $ret = $wheat->open(self::$room_id,$data['wheat_id']);
            //点单房间开麦后启动自动上麦
            if (self::$roomInfo['type'] == 3){
                self::autoWheat(self::$room_id,$data['wheat_id']);
            }
        }

        if($ret['code']){
            $this->refreshWheat(self::$room_id);

            api_return(1,$ret['msg']);

        }else{
            api_return(0,$ret['msg']);
        }

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 检查用户是否为房主
     */
    protected function owner($user_id = null){

        if (!$user_id){
            $user_id = $this->user_id;
        }
        if ($user_id != self::$roomInfo['user_id']){
            return false;
        }
        return true;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 选项
     */
    public function option()
    {
        $wheat = new Wheat();
        $wheatInfo = $wheat->getWheat(self::$room_id);
        $data['show_house'] = $wheatInfo[0]['is_show']??0;
        $data['show_admit'] = $wheatInfo[1]['is_show']??0;
        $data['show_boss']  = $wheatInfo[8]['is_show']??0;
        $rank = cache('rank_'.self::$room_id);
        $data['show_rank']  = $rank?$rank:0;
        $data['is_free']    = self::$is_free;
        api_return(1,'获取成功',$data);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 选项状态切换
     */
    public function changeOption()
    {
        $this->checkPower();

        $data = input('post.');
        if ($data['is'] != 1 && !is_zero($data['is']) ){
            api_return(0,'状态错误');
        }
        $index = 'nothing';
        switch ($data['type']){
            case 'rank':
                $result = cache('rank_'.self::$room_id,$data['is']);
                break;
            case 'free':
                $result = Db::name('room')->where('room_id',self::$room_id)->update(['is_free'=>$data['is']]);
                $this->sendRoomMsg(self::$room_id,'free'.$data['is']);
                break;
            case 'house':
                $index = 0;
                break;
            case 'admit':
                $index = 1;
                break;
            case 'boss':
                $index = 8;
                break;
            default:
                api_return(0,'类型错误');
                break;
        }

        if (is_numeric($index)){
            $class = new Wheat();
            $wheat = $class->getWheat(self::$room_id);
            $wheat[$index]['is_show'] = $data['is'];
            $result = $class->setWheat(self::$room_id,$wheat);
        }
        $this->refreshWheat(self::$room_id);
        if ($result){
            api_return(1,'操作成功');
        }
        api_return(0,'操作失败');

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 修改房间麦位排序
     */
    public function changeSort()
    {
        $class = new Wheat();
        $wheat = $class->wheat(self::$room_id);
        $json  = input('post.wheat');
        $array = json_decode($json,true);

        if (!is_array($array)){
            api_return(0,'类型错误');
        }

        if (count($wheat) != count($array)){
            api_return(0,'参数错误');
        }

        foreach ($array as $k => $v){
            $index = $v['wheat_id']-1;
            if ($index == 0){
                continue;
            }else{
                if ($v['sort'] != 1){
                    $wheat[$index]['sort'] = $v['sort'];
                }
            }
        }

        $class->setWheat(self::$room_id,$wheat);
        $this->refreshWheat(self::$room_id);
        api_return(1,'操作成功');

    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 麦上打赏统计状态修改
     */
    public function count()
    {
        $this->checkPower();

        $type = input('status');
        $class = new Wheat();
        $wheat = $class->getWheat(self::$room_id);
        if ($type != 1 && $type != 2 && !is_zero($type) && $type != 3){
            api_return(0,'状态错误');
        }
        if (is_numeric(input('wheat_id'))){
            $index = input('wheat_id')-1;
            switch ($type){
                case 1:
                    if ($wheat[$index]['status'] == 1){
                        api_return(0,'统计已开始');
                    }
                    $wheat[$index]['status'] = 1;
                    break;
                case 0:
                    $wheat[$index]['count'] = '0';
                    $wheat[$index]['countNum'] = 0;
                    $wheat[$index]['status']   = 0;
                    break;
                case 3:
                    $wheat[$index]['count'] = '0';
                    $wheat[$index]['countNum'] = 0;
                    break;
                case 2:

                    if ($wheat[$index]['status'] != 1){
                        api_return(0,'统计未开始');
                    }
                    $wheat[$index]['status'] = 2;
                    break;

                default:
                    api_return(0,'系统错误');
                    break;
            }
        }else{
            $status = $type;
            foreach ($wheat as $k =>$v){
                if ($status == 0 || $status == 3){
                    if ($status == 3){
                        $type = 1;
                    }
                    $wheat[$k]['count'] = "0";
                    $wheat[$k]['countNum'] = 0;
                }
                $wheat[$k]['status'] = (int) $type;
            }
        }

        $result = $class->setWheat(self::$room_id,$wheat);
        if ($result){
            $this->refreshWheat(self::$room_id);

            api_return(1,'操作成功');

        }
        api_return(0,'操作失败');
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 非房主权限拦截
     */
    protected function checkOwner($user_id = null){
        if (!$user_id){
            $user_id = $this->user_id;
        }

        if (!$this->owner($user_id)){
            api_return(0,'您不是房主,不能进行操作');
        };
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取管理权限代码
     */
    protected function power($user_id = null)
    {
        if (!$user_id){
            $user_id = $this->user_id;
        }
        $power = self::roomPower(self::$room_id,$user_id);
        if ($power == 0 || $power == 1){
            return false;
        }
        return $power;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     *  非管理操作拦截 为管理返回权限代码
     */
    protected function checkPower($user_id = null,$msg = '权限不足,不能操作')
    {
        if (!$user_id){
            $user_id = $this->user_id;
        }
        $power = $this->getPowerCode($user_id);

        if (!$power){
            api_return(0,$msg);
        }
        return $power;

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @param null $user_id
     * @return bool|int|mixed
     * 获取权限操作码
     */
    protected function getPowerCode($user_id = null){

        if (!$user_id){
            $user_id = $this->user_id;
        }
        return getPowerCode($user_id,self::$room_id);
//        $owner = $this->owner($user_id);
//        if ($owner){
//            $power = 1;
//        }else{
//            $power = $this->power($user_id);
//        }
//        if (!$power){
//            $power = 0;
//        }
//        return $power;
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 下麦|踢下麦
     */
    public function downWheat()
    {

        $data = request()->only(['wheat_id','type'],'post');

        $wheat = new Wheat();

        if ($data['type'] == 1){ //踢下麦

            $this->checkPower();

        }else{ //自己下麦

            $info = $wheat->getWheat(self::$room_id)[$data['wheat_id']-1];

            if ($info['user_id'] !== hashid($this->user_id)){
                api_return(0,'您不在麦位上,不能下麦');
            }

        }

        if ($data['wheat_id'] == 1){
            //电台主播下麦清空麦序
            $rows = Wheat::wheatInit(self::$room_id);
            $this->refreshWheat(self::$room_id);
            api_return(1,'操作成功',$rows);
        }

        $ret = $wheat->down(self::$room_id,$data['wheat_id']);

        if($ret['code']){

            if ($data['type'] == 1){ //踢下麦
                $this->sendRoomMsg(self::$room_id,'down',['wheat_id'=>$data['wheat_id']]);
            }
            if(self::$roomInfo['type'] == 2 && self::$roomInfo['type'] == 4 ){

                $this->autoWheat(self::$room_id,$data['wheat_id']);
            }

            $this->refreshWheat(self::$room_id);

            api_return(1,$ret['msg']);

        }else{
            api_return(0,$ret['msg']);
        }
    }

    /**
     * 上麦、换麦、抱麦
     * */
    public function upWheat(){

        $post = request()->only(['wheat_id','type','user_id'],'post');
        //验证数据
        $result = $this->validate($post,'Wheat.up');
        if(true !== $result){
            api_return(0,$result);
        }
        //不传默认登录用户ID
        if(!empty($post['user_id'])){
            $post['user_id'] = dehashid($post['user_id']);
            if (!is_numeric($post['user_id'])) api_return(0,'参数错误');
        }

        $wheat = new Wheat();
        if(isset($post['type']) && $post['type']  == 1){//抱麦

            $this->checkPower($this->user_id);

            if ($post['wheat_id'] == 1){
                api_return(0,'主播位不能抱麦');
            }

            $ret = $wheat->embrace($post['user_id'],self::$room_id,$post['wheat_id'],getPowerCode($post['user_id'],self::$room_id));
        }else{//上麦

            $powerCode = $this->getPowerCode();
            if (self::$is_free){
                if (in_array($post['wheat_id'],self::$generalNotUp)){
                    if (!$powerCode){
                        api_return(0,'非管理员不能上麦');
                    }
                }
            }else{
                if ($post['wheat_id'] != 1){
                    api_return(0,'上麦需申请');
                }
            }

            if ($post['wheat_id'] == 1){
                $this->checkPower($this->user_id,'权限不足,不能上主播位');
            }
            $ret = $wheat->on($this->user_id,self::$room_id,$post['wheat_id'],getPowerCode($this->user_id,self::$room_id));
        }
        if($ret['code']){
            $this->refreshWheat(self::$room_id);

            api_return(1,$ret['msg'],$ret['data']['wheat']);

        }else{
            api_return(0,$ret['msg']);
        }
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 拉人入黑名单
     */
    protected function black($id,$room_id,$time = 600){
        $map['user_id']  = $id;
        $map['room_id']  = $room_id;

        $model = new Logic();
        $model->changeTable('room_blacklist');

        $data = $model->where($map)->find();

        if ($data['status'] == 1 && $data['end_time'] > time()){
            api_return(0,'该用户已在黑名单中');
        }

        $minute = $time/60;
        if ($data){
            $save['status']   = 1;
            $save['end_time'] = time() + $time;
            $save['minute']   = $minute;
            $result = $data->save($save);
        }else{
            $map['end_time'] = time() + $time;
            $map['minute']   = $minute;
            $result = $model->save($map);
        }
        if ($result){
            //踢出房间后更新用户扩展为未进入房间
            self::updateUserExtend($id,['room_id'=> 0 ]);
            self::outRoomFunc($id,self::$room_id);
            $this->refreshWheat(self::$room_id);

            $content = '<tag>'.$this->userInfo('nick_name').
                '<tag> 把  <tag>'.$this->userInfo('nick_name',$id).
                '<tag> 踢出房间'.$minute.'分钟';

            $this->writeLog($content,$room_id);

            $this->sendRoomMsg($room_id,'addBlack',['user_id'=>hashid($id)]);


            api_return(1,'操作成功');
        }
        api_return(0,'操作失败');
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 写入房间操作日志
     */
    protected function writeLog($content,$room_id)
    {
        //写入操作日志
        $log['create_time'] = time();
        $log['update_time'] = time();
        $log['room_id']     = $room_id;
        $log['content']     = $content;
        Db::name('room_log')->insert($log);

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 将用户拉入黑名单
     */
    public function addBlack()
    {

        $data = request()->only(['id','time']);

        $id = dehashid($data['id']);

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }

        if (!isInt($data['time'])){
            api_return(0,'封禁时间错误');
        }

        $this->checkPower();

        $userPowerCode = $this->getPowerCode($id);

//        if ($userPowerCode == 4){
//            api_return(0,'权限不足');

//        $userPowerCode = $this->getPowerCode($id);
//
//        if ($userPowerCode != 4){
//            api_return(0,'您不能将房主或房间管理踢出房间');
//        }


        $noble_id = self::checkNoble($this->userExtra('noble_id,noble_time,user_id',$id));

        $array = [3,4,5];

        if (in_array($noble_id,$array)){

            $name = getNobleName($noble_id);

            api_return(0,$name.'不能被踢出房间');
        }

        $this->black($id,self::$room_id,$data['time']);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 设为房间管理
     */
    public function roomManage()
    {
        $hash   = input('post.id');
        $id     = dehashid($hash);
        $status = input('post.status')??2;

        if ($status != 2 && $status != 3){
            api_return(0,'参数错误');
        }

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }

        $userPower = $this->checkPower();

        if ($userPower != 1){
            if ($status == 2){
                api_return(0,'权限不足,不能设置高级管理员');
            }
        }

        $powerCode = $this->getPowerCode($id);

        if ($powerCode == $status){
            api_return(0,'该用户已是房间管理,不能重复操作');
        }

        $map['room_id'] = self::$room_id;
        $map['user_id'] = $id;

        $model = new Logic();
        $model->changeTable('room_follow');

        $data = $model->where($map)->find();

        if ($data){
            $result = $data->save(['status'=>$status]);
        }else{
            $map['status'] = $status;
            $result = $model->save($map);
        }

        if ($result){
            if ($status == 2){
                $name = '接待';
            }else{
                $name = '管理员';
            }

            $str = sprintf('%s在房间[%s]内将你设为了%s',$this->userInfo('nick_name'),self::$roomInfo['room_name'],$name);
            //发送系统消息
            self::sendUsrMsg('system',$hash,$str,['type'=>'createRoom','data'=>self::$room_id]);

            $this->sendRoomMsg(self::$room_id,'manage',['user_id'=>hashid($id),'status'=>$status]);
            //写入操作日志
            $content = '<tag>'.$this->userInfo('nick_name').
                '<tag> 把  <tag>'.$this->userInfo('nick_name',$id).
                '<tag> 设为'.$name;

            $this->writeLog($content,self::$room_id);
            //
            $this->refreshWheat(self::$room_id,true);
            api_return(1,'操作成功');
        }
        api_return(0,$data->getError().$model->getError());
    }
    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 取消房间管理
     */
    public function cancelManage()
    {
        $hash = input('post.id');
        $id   = dehashid($hash);

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }

        $this->checkPower();

        $powerCode = $this->getPowerCode($id);

        if (!$powerCode){
            api_return(0,'该用户不是房间管理,不能操作');
        }

        $map['room_id'] = self::$room_id;
        $map['user_id'] = $id;

        $model = new Logic();
        $model->changeTable('room_follow');

        $data = $model->where($map)->find();

        if ($data){
           $data->save(['status'=>1]);
        }else{
            api_return(0,$data->getError());
        }

        $str = sprintf('%s取消了你在房间[%s]的管理员权限',$this->userInfo('nick_name'),self::$roomInfo['room_name']);
        //发送系统消息
        self::sendUsrMsg('system',$hash,$str,['type'=>'createRoom','data'=>self::$room_id]);

        //写入操作日志
        $content = '<tag>'.$this->userInfo('nick_name').
            '<tag> 取消  <tag>'.$this->userInfo('nick_name',$id).
            '<tag> 的管理权限';
        $this->sendRoomMsg(self::$room_id,'removeManage',['user_id'=>hashid($id)]);
        $this->writeLog($content,self::$room_id);
        $this->refreshWheat(self::$room_id,true);
        api_return(1,'操作成功');

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 将用户移出黑名单
     */
    public function removeBlack()
    {
        $hash = input('post.id');
        $id = dehashid($hash);

        if (!is_numeric($id)){
            api_return(0,'用户id错误');
        }

        $this->checkPower();

        $map['user_id']  = $id;
        $map['room_id']  = self::$room_id;

        $model = new Logic();
        $model->changeTable('room_blacklist');

        $data = $model->where($map)->find();

        if ($data['status'] != 1 || $data['end_time'] < time()){
            api_return(0,'该用户不在黑名单中');
        }

        $save['status']   = 0;
        $save['end_time'] = 0;
        $result = $data->save($save);

        if ($result){

            //写入操作日志
            $content = '<tag>'.$this->userInfo('nick_name').
                '<tag> 把  <tag>'.$this->userInfo('nick_name',$id).
                '<tag> 移出封禁列表';

            $str = sprintf('%s将你从房间[%s]的黑名单中移除',$this->userInfo('nick_name'),self::$roomInfo['room_name']);
            //发送系统消息
            self::sendUsrMsg('system',$hash,$str);
            $this->writeLog($content,self::$room_id);

            api_return(1,'操作成功');
        }
        api_return(0,'操作失败');

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间管理列表
     */
    public function manageList()
    {
        $map['a.status']  = ['between','2,3'];
        $map['a.room_id'] = self::$room_id;
        $model = new RoomFollow();
        $rows = $model->getRows($map);
        api_return(1,'获取成功',$rows);
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @param int $id
     * 修改背景图片
     */
    public function bgChange(int $id)
    {
        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足,不能修改背景图片');

        $bg = RoomBackground::getBg($id,false);

        if (!$bg) api_return(0,'参数错误');

        if ($bg['bg_id'] == self::$roomInfo['bg_id']) api_return(0,'请选择其它房间背景');

        $result = $this->_changeField('room',self::$room_id,'bg_id',$id);

        if ($result){
            //写入操作日志
            $content = '<tag>'.$this->userInfo('nick_name').
                '<tag> 修改房间背景为 '.$bg['brief'];
            $this->writeLog($content,self::$room_id);

            //通知到房间内房间信息有修改
            $room = Db::name('room')
                ->alias('a')
                ->join('room_background b','b.bg_id = a.bg_id','LEFT')
                ->join('room_category c','c.cid = a.cid','LEFT')
                ->where('a.room_id',self::$room_id)
                ->field('a.notice,b.img as bg_img,a.room_name,a.is_lock,a.password,a.img,a.cid,c.cate_name')
                ->find();
            $this->sendRoomMsg(self::$room_id,'changeRoom',$room);

            api_return(1,'修改成功');
        }

        api_return(0,'修改失败');
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 禁言
     */
    public function banned()
    {

        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足,不能修改禁言用户');

        $data = request()->only(['id','time'],'post');

        if (!is_numeric(dehashid($data['id']))){
            api_return(0,'用户id错误');
        }

        if (!is_numeric(trim($data['time']))){
            api_return(0,'禁言时间错误');
        }
        $end_time = $data['time'] * 60 + time();
        $model = new Logic();
        $model->changeTable('room_baned');

        $item['user_id'] = dehashid($data['id']);
        $item['room_id'] = self::$room_id;

        $row = $model->where($item)->find();

        if (!$row){
            $item['end_time'] = $end_time;
            $result = $model->save($item);
        }else{
            if ($row['status'] == 1 && $row['end_time'] > time()){
                api_return(0,'用户已被禁言');
            }else{
                $result = $row->save(['status'=>1,'end_time'=>$end_time]);
            }
        }

        if ($result !== false){
            $this->sendRoomMsg(self::$room_id,'baned',['user_id'=>$data['id'],'time'=>$data['time']]);
            api_return(1,'禁言成功');
        }
        api_return(0,'禁言失败');

    }

    /**
     * 取消禁言
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function cancelBanned()
    {

        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足,不能修改禁言用户');

        $data = request()->only(['id'],'post');

        if (!is_numeric(dehashid($data['id']))){
            api_return(0,'用户id错误');
        }

        $model = new Logic();
        $model->changeTable('room_baned');

        $item['user_id'] = dehashid($data['id']);
        $item['room_id'] = self::$room_id;

        $row = $model->where($item)->find();

        if (!$row){
            api_return(0,'用户未被禁言');
        }else{
            if ($row['status'] == 1 && $row['end_time'] > time()){
                $result = $row->save(['status'=>0,'end_time'=>0]);
            }else{
                api_return(0,'用户未被禁言');
            }
        }

        if ($result !== false){
            $this->sendRoomMsg(self::$room_id,'cancelBaned',['user_id'=>$data['id']]);
            api_return(1,'操作成功');
        }
        api_return(0,'操作失败');

    }

    /**
     * 禁言用户列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function bandList()
    {
        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足');
        $map['a.status']   = 1;
        $map['a.room_id']  = self::$room_id;
        $map['a.end_time'] = ['>',time()];

        $rows = Db::name('room_baned')
            ->alias('a')
            ->join('users u','u.user_id = a.user_id','left')
            ->join('user_extend e','e.user_id = a.user_id','left')
            ->field('u.nick_name,u.header_img,a.user_id,a.end_time,e.level,e.noble_id,e.noble_time')
            ->cache(2)
            ->where($map)
            ->select();
        foreach ($rows as $k => $v){
            $rows[$k]['noble_id'] = self::checkNoble($v);
            $rows[$k]['user_id']  = hashid($v['user_id']);
            $rows[$k]['end_time'] = formatTime($v['end_time'],false);
        }
        api_return(1,'获取成功',$rows);

    }



    /**
     * 房间麦位语音关闭或开启
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function voice()
    {
        $data = input('post.');
        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足,不能操作');
        $wheat = new Wheat();

        $rows = $wheat->getWheat(self::$room_id);

        if ($rows[$data['wheat_id']-1]){
            if ($data['is_close'] != 1 && !is_zero($data['is_close']) ){
                api_return(0,'参数错误');
            }
            $rows[$data['wheat_id']-1]['is_close'] = $data['is_close'];
            $wheat->setWheat(self::$room_id,$rows);
            self::refreshWheat(self::$room_id);

            api_return(1,'操作成功');

        }
        api_return(0,'wheat_id参数错误');
    }

    /**
     * 房间倒计时开启或关闭
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function down()
    {
        $data = input('post.');
        $powerCode = $this->getPowerCode();
        if ($powerCode === 0) api_return(0,'权限不足,不能操作');
        $wheat = new Wheat();

        if (!is_numeric($data['time'])){
            api_return(0,'时间错误');
        }
        $rows = $wheat->getWheat(self::$room_id);

        if ($rows[$data['wheat_id']-1]){
            if ($data['time'] == 0){
                $rows[$data['wheat_id']-1]['down'] = 0;
            }elseif ($rows[$data['wheat_id']-1]['down'] > time()){
                $rows[$data['wheat_id']-1]['down'] = $rows[$data['wheat_id']-1]['down']+$data['time'];
            }else{
                $rows[$data['wheat_id']-1]['down'] = time() + $data['time'];
            }

            $wheat->setWheat(self::$room_id,$rows);
            self::refreshWheat(self::$room_id);

            api_return(1,'操作成功');

        }
        api_return(0,'wheat_id参数错误');
    }




}